package com.example.takeaway.util;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author 邢晨旭
 * {@code @date} 2023/10/27
 */
public class RedisUtil {
    private static final StringRedisTemplate STRING_REDIS_TEMPLATE;

    static {
        STRING_REDIS_TEMPLATE = SpringUtil.getBean(StringRedisTemplate.class);
    }

    public static void set(String key, String value) {
        STRING_REDIS_TEMPLATE.opsForValue().set(key, value);
    }

    public static void set(String key, String value, long timeout) {
        STRING_REDIS_TEMPLATE.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    public static void set(String key, String value, long timeout, TimeUnit timeUnit) {
        STRING_REDIS_TEMPLATE.opsForValue().set(key, value, timeout, timeUnit);
    }

    public static String get(String key) {
        return STRING_REDIS_TEMPLATE.opsForValue().get(key);
    }

    public static void delete(String key) {
        STRING_REDIS_TEMPLATE.delete(key);
    }
}
